'use strict';

const softDelete = require('mongoosejs-soft-delete');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const AutoConf = new Schema(
    {
      name: { type: String, required: true },
      title: { type: String, required: true },
      // 0: 非列表，1: 列表
      type: { type: Number, enum: [ 0, 1 ], default: 0 },
      content: { type: [ Object ], default: [] },
      extend: { type: Object, default: { router: '' } },
      createdAt: { type: Date, immutable: true },
      updatedAt: { type: Date, select: false },
    },
    { timestamps: true, versionKey: false }
  );

  AutoConf.virtual('Conf', {
    ref: 'AppConfig',
    localField: 'name',
    foreignField: 'type',
    justOne: true,
  });

  AutoConf.index({ name: 1 }, { unique: true });
  AutoConf.plugin(softDelete);
  return app.buildValidateOfModel(mongoose.model('AutoConf', AutoConf));
};
